package com.zzsoft.nettysocket;/**
 * Created by zhanghb on 2019/9/3.
 */

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

/**
 * @Description 保持socket长连接客户端代码
 * @Author Zhanghb
 * @Date
 */
public class SokcketClient {
        private Socket socket = null;
        private OutputStream os = null;
        private InputStream is = null;

        public static void main(String[] args) {
            new SokcketClient().new SocketThread().start();
        }

        /**
         * 发送心跳包
         */
        public void sendHeartbeat() {
            try {
                String heartbeat = "key00001";//"heart:00001;";
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        while (true) {
                            try {
                                Thread.sleep(20 * 1000);// 10s发送一次心跳
                                os.write(heartbeat.getBytes());
                                os.flush();
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }).start();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        private class SocketThread extends Thread {

            @Override
            public void run() {
                long startTime = System.currentTimeMillis();
                sendHeartbeat();
                while (true) {
                    try {
                        if (socket == null || socket.isClosed()) {
                            socket = new Socket("localhost", 8001); // 连接socket
                            os = socket.getOutputStream();
                        }
                        Thread.sleep(100);
                        is = socket.getInputStream();
                        int size = is.available();
                        if (size <= 0) {
                            if ((System.currentTimeMillis() - startTime) > 3 * 10 * 1000) { // 如果超过30秒没有收到服务器发回来的信息，说明socket连接可能已经被远程服务器关闭
                                socket.close(); // 这时候可以关闭socket连接
                                startTime = System.currentTimeMillis();
                            }
                            continue;
                        } else {
                            startTime = System.currentTimeMillis();
                        }
                        byte[] resp = new byte[size];
                        is.read(resp);
                        String response = new String(resp, "utf-8");
                        System.out.println(response);
                    } catch (Exception e) {
                        e.printStackTrace();
                        try {
                            socket.close();
                            is.close();
                            os.close();
                        } catch (IOException e1) {
                            e1.printStackTrace();
                        }
                    }
                }
            }
        }

}
